import 'dart:io';
import 'package:path/path.dart' as path;
import 'lib/asr_ffi.dart';

void main() async {
  print('========== 部署测试脚本 ==========');

  // 显示当前运行环境
  print('当前工作目录: ${Directory.current.path}');
  print('可执行文件路径: ${Platform.resolvedExecutable}');
  print('可执行文件目录: ${path.dirname(Platform.resolvedExecutable)}');

  try {
    // 获取运行时目录
    final runtimeDir = AsrFFI.getRuntimeDirectory();
    print('预期运行时目录: $runtimeDir');

    // 获取部署诊断信息
    print('\n========== 获取诊断信息 ==========');
    final diagnosticInfo = await AsrFFI.getDeploymentDiagnostic();

    // 显示平台信息
    print('\n【平台信息】');
    final platform = diagnosticInfo['platform'] as Map;
    platform.forEach((key, value) {
      print('  $key: $value');
    });

    // 显示目录信息
    print('\n【目录结构】');
    final directories = diagnosticInfo['directories'] as Map;
    directories.forEach((key, value) {
      print('  $key: $value');
    });

    // 显示可执行文件状态
    print('\n【可执行文件状态】');
    final executables = diagnosticInfo['executables'] as Map;
    executables.forEach((fileName, info) {
      final fileInfo = info as Map;
      print('  $fileName:');
      print('    路径: ${fileInfo['path']}');
      print('    存在: ${fileInfo['exists']}');
      print('    大小: ${fileInfo['size']} 字节');
      print('    可执行: ${fileInfo['canExecute']}');
    });

    // 显示DLL文件状态
    print('\n【DLL文件状态】');
    final dlls = diagnosticInfo['dlls'] as Map;
    dlls.forEach((fileName, info) {
      final fileInfo = info as Map;
      print('  $fileName:');
      print('    路径: ${fileInfo['path']}');
      print('    存在: ${fileInfo['exists']}');
      print('    大小: ${fileInfo['size']} 字节');
    });

    // 显示模型文件状态
    print('\n【模型文件状态】');
    final models = diagnosticInfo['models'] as Map;
    final availableModels = models['availableModels'] as List?;
    print('  可用模型: ${availableModels?.join(', ') ?? '无'}');

    if (availableModels != null) {
      for (final modelName in availableModels) {
        if (models[modelName] is Map) {
          final modelInfo = models[modelName] as Map;
          print('  $modelName:');
          print('    路径: ${modelInfo['path']}');
          print('    文件数: ${(modelInfo['files'] as List).length}');
          print('    有ONNX模型: ${modelInfo['hasOnnxModel']}');
          print('    有配置文件: ${modelInfo['hasConfigYaml']}');
          print('    有Token文件: ${modelInfo['hasTokens']}');
        }
      }
    }

    print('\n========== 诊断完成 ==========');
  } catch (e, stackTrace) {
    print('❌ 诊断过程中出现错误: $e');
    print('堆栈跟踪: $stackTrace');
  }
}
